package com.zygh.policeasset.mapper;

import com.zygh.policeasset.pojo.AssetsUpdate;
import com.zygh.policeasset.pojo.ProjectBudget;
import com.zygh.policeasset.pojo.vo.AssetsUpdateVO;
import com.zygh.policeasset.pojo.vo.ProjectBudgetVO;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

@Mapper
public interface AssetsUpdateMapper {

    @Select({"select * from assets_update"})
    @Results(id = "AssetTransferMap", value = {
            @Result(column = "asset_id", property = "assetId", jdbcType = JdbcType.INTEGER),
            @Result(column = "image_url", property = "imageUrl", jdbcType = JdbcType.VARCHAR),
            @Result(column = "flow_no", property = "flowNo", jdbcType = JdbcType.VARCHAR)})
    List<AssetsUpdate> findAll();

    @Insert({"insert into assets_update (asset_id,place,use_user,image_url,flow_no,create_time) " +
            "values (#{assetId},#{place},#{useUser},#{imageUrl},#{flowNo},now())"})
    Integer add(AssetsUpdate assetsUpdate);

    @Delete({"delete from assets_update where id=#{id}"})
    Integer delete(Integer id);

    @Update({"<script>" +
            "update assets_update " +
            "<set>" +
            "<if test='assetId != null '>" +
            "asset_id = #{assetId}, " +
            "</if>" +
            "<if test='place != null '>" +
            "place = #{place}, " +
            "</if>" +
            "<if test='useUser != null '>" +
            "use_user = #{useUser}, " +
            "</if>" +
            " update_time = now() " +
            "</set> where id = #{id}" +
            "</script>"})
    Integer update(AssetsUpdate assetsUpdate);

    @Select({"select au.*,af.app_status as appStatus,af.approval_steps_id as approvalStepsId," +
            "u.name as userName from assets_update au " +
            "left join audit_flow af on au.flow_no = af.flow_no " +
            " left join user u on af.applicant = u.id  " +
            "where au.id = #{id}"})
    @Results(value = {
            @Result(column = "asset_id", property = "assetId", jdbcType = JdbcType.INTEGER),
            @Result(column = "image_url", property = "imageUrl", jdbcType = JdbcType.VARCHAR),
            @Result(column = "use_user", property = "useUser", jdbcType = JdbcType.VARCHAR),
            @Result(column = "flow_no", property = "flowNo", jdbcType = JdbcType.VARCHAR)})
    AssetsUpdateVO findById(Integer id);

    @Select({"select count(*) from assets_update au left join audit_flow af on au.flow_no = af.flow_no " +
            "where af.app_status != 2 and au.asset_id = #{id}"})
    Integer findCountById(Integer id);

    @Select({"select * from assets_update where flow_no = #{flowNo}"})
    @ResultMap("AssetTransferMap")
    AssetsUpdate findByIdFlowNo(String flowNo);
}